package com.fsj.miniapp.service.impl;

import com.fsj.miniapp.entity.User;
import com.fsj.miniapp.service.UserService;
import com.stip.mybatis.generator.plugin.BaseExample;
import com.stip.mybatis.generator.plugin.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl extends BaseService<User, BaseExample, Long> implements UserService {

    @Value("${security.user.pwd.salt}")
    private String salt;


    @Override
    public com.fsj.miniapp.model.User findUserByName(String username) {
        com.fsj.miniapp.model.User user = new com.fsj.miniapp.model.User();

        BaseExample example = new BaseExample();

        example.createCriteria().addCriterion("user_name =", username)
        .addCriterion("mark_for_delete = 0");
        List<User> users = selectByExample(example);
        if (!CollectionUtils.isEmpty(users)) {
            BeanUtils.copyProperties(users.get(0), user);
            user.setSalt(user.getUserName()+salt);
            return user;
        }
        return null;
    }

    @Override
    public void addUser(String uname, String hashPassword) {
        User user = new User();
        user.setAccount(uname);
        user.setCreator(10001l);
        user.setCreatedDate(new Date());
        user.setMarkForDelete(0);
        user.setPassword(hashPassword);
        user.setUserName(uname);
        insertSelective(user);
    }

    @Override
    public Set<String> getAllFuncsByUserId(Long id) {

        return null;
    }
}